home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Dreamweaver 3 / Configuration / Shared / MM / Scripts / CMN / UI.js < prev   
Encoding:
Text File  |  1999-12-01  |  5.6 KB  |  180 lines

  1. //
  2. // Copyright 1999 Macromedia, Inc. All rights reserved.
  3. //
  4. //UI.js
  5. //
  6. //Generic functions that control the UI.
  7. //Functions specific to controlling menu item text is stored in menuItem.js
  8. //
  9. //--------------------------------------------------------------
  10. //
  11. //
  12. //findObject(objName,  parentObj)
  13. //loadSelectList()
  14.  
  15.  
  16. //given an object name, returns an obj in current document or frameset
  17. //with that name
  18.  
  19. function findObject(objName,  parentObj) {
  20.   var i,tempObj="",found=false,curObj = "";
  21.     parentObj = (parentObj != null)? parentObj.document : document;
  22.     if (parentObj[objName] != null) curObj = parentObj[objName]; //at top level
  23.     else { //if in form
  24.       if (parentObj.forms) for (i=0; i<parentObj.forms.length; i++) {  //search level for form object
  25.         if (parentObj.forms[i][objName]) {
  26.           curObj = parentObj.forms[i][objName];
  27.           found = true; break;
  28.       } }
  29.       if (!found && parentObj.layers && parentObj.layers.length > 0) {
  30.         parentObj = parentObj.layers;
  31.         for (i=0; i<parentObj.length; i++) { //else search for child layers
  32.           tempObj = findObject(objName,parentObj[i]); //recurse
  33.           if (tempObj) { curObj = tempObj; break;} //if found, done
  34.   } } } 
  35.   return curObj;
  36. }
  37.  
  38. //the version of findObject we use in Dreamweaver is only for controlling the UI
  39. //in extension files.
  40. //If inserting findObject into a user's document, use the commented out
  41. //version below.
  42.  
  43. /*
  44. function findObject(objName,  parentObj) {
  45.   var i,tempObj="",found=false,curObj = "";
  46.   var NS = (navigator.appName.indexOf("Netscape") != -1);
  47.   if (!NS && document.all) curObj = document.all[objName]; //IE4
  48.   else {
  49.     parentObj = (parentObj != null)? parentObj.document : document;
  50.     if (parentObj[objName] != null) curObj = parentObj[objName]; //at top level
  51.     else { //if in form
  52.       if (parentObj.forms) for (i=0; i<parentObj.forms.length; i++) {  //search level for form object
  53.         if (parentObj.forms[i][objName]) {
  54.           curObj = parentObj.forms[i][objName];
  55.           found = true; break;
  56.       } }
  57.       if (!found && NS && parentObj.layers && parentObj.layers.length > 0) {
  58.         parentObj = parentObj.layers;
  59.         for (i=0; i<parentObj.length; i++) { //else search for child layers
  60.           tempObj = findObject(objName,parentObj[i]); //recurse
  61.           if (tempObj) { curObj = tempObj; break;} //if found, done
  62.   } } } }
  63.   return curObj;
  64. }*/
  65.  
  66.  
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // Function
  70. //    loadSelectList
  71. //
  72. // Purpose
  73. //    In order to simplify localizations, the text strings for select
  74. //    list options are listed at the top of the .htm files in the
  75. //    Localizable Global Variables section.  When the dialog is loaded,
  76. //    the select widget options are dynamically updated with the
  77. //    localized text strings by calling loadSelectList().
  78. //    
  79. // Parameters
  80. //    selectListObj - the select object to populate
  81. //    locallizedArr - the array of strings for the names of the options.
  82. //
  83. // Optional Parameters
  84. //    containsValues - boolean indicating whether the string array
  85. //       also contains values.  true if it does, false if not.  If
  86. //       it contains values, the array is in the format of option
  87. //       name, value, option name, value...
  88. //    selectedIndex - the option to select initially
  89. //
  90. function loadSelectList(selectListObj, localizedArr)
  91.   // Optional params: containsValues,  selectedIndex
  92. {
  93.    var arrLen = localizedArr.length;
  94.    var containsValues, selectedIndex;
  95.    var i;
  96.    
  97.    if(loadSelectList.arguments.length >= 3)
  98.       containsValues = loadSelectList.arguments[2];
  99.    else
  100.       containsValues = false;
  101.  
  102.    if(loadSelectList.arguments.length >= 4)
  103.       selectedIndex = loadSelectList.arguments[3];
  104.    else
  105.       selectedIndex = 0;
  106.    
  107.    if(containsValues)
  108.    {
  109.       // The array contains values for each list item.  We
  110.       // need to set the values too.
  111.       for(i = 0; i < arrLen/2; i++)
  112.       {
  113.          selectListObj.options[i] = new Option(localizedArr[i*2]);
  114.          selectListObj.options[i].value = localizedArr[i*2+1];
  115.       }
  116.    }
  117.    else
  118.    {
  119.       for(i = 0; i < arrLen; i++)
  120.          selectListObj.options[i] = new Option(localizedArr[i]);
  121.    }
  122.    
  123.    // Select one of the items by default
  124.    selectListObj.selectedIndex = selectedIndex;
  125. }
  126.  
  127.  
  128.  
  129. //function: getSelectedOptionAttr
  130. //description: given a select object and an attribute, returns the value of that
  131. //attribute for the selected option
  132.  
  133. function getSelectedOptionAttr(selectListObj, attr){
  134.    var selInd = selectListObj.selectedIndex;
  135.    return selectListObj.options[selInd][attr];
  136. }
  137.  
  138. /////////////////////////////////////////////////////////////////////////////
  139. // Function
  140. //    wrapTextForAlert
  141. //
  142. // Purpose
  143. //    Alert dialog boxes take the string that you give and display it.
  144. //    However, it does not wrap the string if it is too long.  So, what
  145. //    you get is an alert box with a really long one line string.  This
  146. //    function will take the specified string and insert carriage
  147. //    returns at the specified columns to "wrap" the string.
  148. //
  149. // Parameters
  150. //    string - the string to wrap
  151. //    cols - integer where to wrap the string (eg 65, 80, 100)
  152. //
  153. // Returns
  154. //    the wrapped string
  155. //
  156. function wrapTextForAlert(str, cols)
  157. {
  158.    var i;
  159.    var ch;
  160.    var lastws = 0;
  161.    var colToWrap = cols;
  162.    var wrapString = (str)?str:'';
  163.    
  164.    for(i = 0; i < wrapString.length; i++)
  165.    {
  166.       ch = wrapString.charAt(i);
  167.       if(ch == ' ' || ch == '\t')
  168.          lastws = i;
  169.  
  170.       if(i >= colToWrap && lastws != 0)
  171.       {
  172.          wrapString = wrapString.substring(0, lastws) + "\n" +
  173.             wrapString.substring(lastws+1);
  174.          colToWrap = lastws + cols;
  175.       }
  176.    }
  177.    
  178.    return wrapString;
  179. }
  180.